package com.eonmind.knowledge.service.impl;

import com.eonmind.common.contract.enums.oss.OssUploadEnum;
import com.eonmind.common.provider.entity.knowledge.KnowledgeUploadFile;
import com.eonmind.knowledge.converts.KnowledgeUploadFileConvert;
import com.eonmind.knowledge.core.BailianKnowledgeService;
import com.eonmind.knowledge.dto.KnowledgeUploadFileDto;
import com.eonmind.knowledge.mapper.KnowledgeMapper;
import com.eonmind.knowledge.mapper.KnowledgeUploadFileMapper;
import com.eonmind.knowledge.service.KnowledgeUploadFileService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @Author: AKang
 * @Description:
 * @CreateTime: 2025-05-28
 */
@Service
public class KnowledgeUploadFileServiceImpl implements KnowledgeUploadFileService {

    @Resource
    private KnowledgeUploadFileMapper knowledgeUploadFileMapper;
    @Resource
    private BailianKnowledgeService bailianKnowledgeService;
    @Resource
    private KnowledgeMapper knowledgeMapper;

    /**
     * 保存知识文件上传信息
     *
     * @param knowledgeFile
     */
    @Override
    public void saveKnowledgeUploadFile(KnowledgeUploadFileDto.KnowledgeFile knowledgeFile) {
        //类型转换
        KnowledgeUploadFile knowledgeUploadFile = KnowledgeUploadFileConvert.toKnowledgeUploadFile(knowledgeFile);
        //文件大小
        Long fileSize = bailianKnowledgeService.getFileSize(OssUploadEnum.KNOWLEDGE_FILE_UPLOAD, knowledgeFile.getRealName(), knowledgeFile.getUserId());
        knowledgeUploadFile.setFileSize(fileSize);
        //知识库ID
        Long knowledgeId = knowledgeMapper.getKnowledgeIdByIndexId(knowledgeFile.getIndexId());
        knowledgeUploadFile.setKnowledgeId(knowledgeId);

        knowledgeUploadFileMapper.saveKnowledgeUploadFile(knowledgeUploadFile);
    }

    /**
     * 级联删除该知识库关联的文件上传信息
     *
     * @param knowledgeId
     */
    @Override
    public void deleteKnowledgeUploadFile(Long knowledgeId) {
        knowledgeUploadFileMapper.deleteKnowledgeUploadFile(knowledgeId);
    }

    /**
     * 更新知识文件上传信息
     *
     * @param knowledgeFile
     */
    @Override
    public void updateKnowledgeUploadFile(KnowledgeUploadFileDto.KnowledgeFile knowledgeFile) {
        KnowledgeUploadFile knowledgeUploadFile = KnowledgeUploadFileConvert.toKnowledgeUploadFile(knowledgeFile);
        knowledgeUploadFile.setFileSize(knowledgeFile.getFileSize());
        knowledgeUploadFileMapper.updateKnowledgeUploadFile(knowledgeUploadFile);
    }
}
